package com.sweather.search;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.*;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * 
 *
 */
public class SearchData 
{
	
	public String weatherUrlBase = "https://tianqi.moji.com/weather/china/hebei/";
	public String aqiUrlBase = "https://tianqi.moji.com/aqi/china/hebei/";
	
	public String Temp;
	public String Weather;
	public String Humidity;
	public String WindInfo;
	public String WeatherTip;
	public String UpdateTime;
	public String WeatherIndex;
	public String WeatherState;
	public String PM10;
	public String PM25;
	public String NO2;
	public String SO2;
	public String O3;
	public String CO;
	public String GetTime;
	public String EnvTime;
	public int id_Address_1;
	public String WeatherImgPath;
	public String address;
	
	static ResultSet res;
      
    public void buildUrl(int Id){
    	
    	   SearchData t = new SearchData();	    
	       conn = t.getConnection();
	       try{
		    	PreparedStatement sql = conn.prepareStatement("select Id_Address from info_address " + " where Id = ?");
		    	sql.setInt(1,Id);
		    	System.out.println(sql);
		    	res = sql.executeQuery();
		    	while(res.next()){
                     address = res.getString("Id_Address");
		    	}
		    	
		    	sql.close();
		        conn.close();
		    }catch(Exception e){
		    	e.printStackTrace();
		    }
	      //  System.out.println(address);
    	    String weatherUrl = weatherUrlBase + address;
    	    String aqiUrl = aqiUrlBase + address;
    	    id_Address_1 = Id;
            this.getData(weatherUrl,aqiUrl,id_Address_1); 
                        
    }
 
    public void getData(String weather_url,String aqi_url,int id_Address_1) {
        try {
        	
            Document weather_doc = Jsoup.connect(weather_url).get();
            Document aqi_doc = Jsoup.connect(aqi_url).get();
            //weather
            Elements Temp_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_weather.clearfix>em");//温度
            Elements Weather_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_weather.clearfix>b");//天气情况
            Elements Humidity_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_about.clearfix>span");//湿度
            Elements WindInfo_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_about.clearfix>em");//风向
            Elements WeatherTip_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_tips.clearfix>em");//天气提示
            Elements UpdateTime_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_weather.clearfix>strong");//更新时间
            Elements WeatherImgPath_dom = weather_doc.select("body>div.wrap.clearfix.wea_info>div.left>div.wea_weather.clearfix>span>img");//image
             Temp = Temp_dom.text();
             Weather = Weather_dom.text();
             Humidity = Humidity_dom.text();
             WindInfo = WindInfo_dom.text();
             WeatherTip = WeatherTip_dom.text();
             UpdateTime = UpdateTime_dom.text();
             WeatherImgPath = WeatherImgPath_dom.toString();
            //aqi
            Elements WeatherIndex_dom = aqi_doc.select("#aqi_value");//空气质量指数
            Elements WeatherState_dom = aqi_doc.select("#aqi_desc");//空气质量
            Elements PM10_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(1)> span");//
            Elements PM25_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(2)> span");//
            Elements NO2_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(3)> span");//
            Elements SO2_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(4)> span");//
            Elements O3_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(5)> span");//
            Elements CO_dom = aqi_doc.select("#aqi_info>div.aqi_info_item>ul>li:nth-child(6)> span");//
            Elements EnvTime_dom = aqi_doc.select("#aqi_info>div.aqi_info_time>b");//更新时间
             WeatherIndex = WeatherIndex_dom.text();
             WeatherState = WeatherState_dom.text();
             PM10 = PM10_dom.text();
             PM25 = PM25_dom.text();
             NO2 = NO2_dom.text();
             SO2 = SO2_dom.text();
             O3 = O3_dom.text();
             CO = CO_dom.text();
             EnvTime = EnvTime_dom.text();
             
            Date date = new Date();
            GetTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
            
            //System.out.println( ":"+ WeatherImgPath);
                                    
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        SearchData t = new SearchData();	    
	    conn = t.getConnection();
	    
	    try{
	    	PreparedStatement sql = conn.prepareStatement("INSERT INTO `info_weather` (`Temp`,`Weather`,`Humidity`, `WindInfo`, `WeatherTip`, `UpdateTime`, `WeatherIndex`, `WeatherState`, `PM10`, `PM25`, `NO2`, `SO2`, `O3`, `CO`, `GetTime`, `EnvTime`,`Id_Address`,`WeatherImgPath`)" + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
	    	sql.setString(1,Temp);
	    	sql.setString(2,Weather);
	    	sql.setString(3,Humidity);
	    	sql.setString(4,WindInfo);
	    	sql.setString(5,WeatherTip);
	    	sql.setString(6,UpdateTime);
	    	sql.setString(7,WeatherIndex);
	    	sql.setString(8,WeatherState);
	    	sql.setString(9,PM10);
	    	sql.setString(10,PM25);
	    	sql.setString(11,NO2);
	    	sql.setString(12,SO2);
	    	sql.setString(13,O3);
	    	sql.setString(14,CO);
	    	sql.setString(15,GetTime);
	    	sql.setString(16,EnvTime);
	    	sql.setInt(17,id_Address_1);
	    	sql.setString(18,WeatherImgPath);
	    	
	    	System.out.println(sql);
	    	sql.executeUpdate();
	    	
	    	sql.close();
	        conn.close();
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    
	    
    }
    
    Connection conn;
    public Connection getConnection(){
    	   
    	try{
    		Class.forName("com.mysql.jdbc.Driver");
    		System.out.println("成功加载MySQL驱动！");
    	}catch(ClassNotFoundException e){
    		System.out.println("找不到MySQL驱动!");
            e.printStackTrace();
    	}
    	
    	String url="jdbc:mysql://192.168.1.245:3306/weathercollection";
    	try{
    		
    		conn = DriverManager.getConnection(url,"root","Root");
            //创建一个Statement对象
            System.out.print("成功连接到数据库！");
    	}catch(SQLException e){
    		e.printStackTrace();
    	}
    	
    	return conn;
    }
    
//    public static void main(String[] args){
//	    int address = 1;
//	    int address1 = 2;
//	    int address2 = 3;
//	    SearchData t = new SearchData();
//	    t.buildUrl(address);  
//	    t.buildUrl(address1);
//	    t.buildUrl(address2);
//    }
    
 
}